clear;
clc;
close all;
addpath('../02_functions');
dbstop if error

%------------------------------------------------------------------------%
% This file creates figure 10(a-c) in the paper 
%------------------------------------------------------------------------%


% load ----------------------------------------------------------------------- 

file_name           = '../01_data/es_quotes_5minute_EST';

% Call returns routine -----------------------------------------------------------------------

start_date          = datenum(1998,5);
finish_date         = datenum(2020,12,29);

winsorizing         = 0;
alpha               = 0.99;
replace_option      = 1;

results             = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
log_return_HbH_AD   = 100*252*results.log_return_HbH_AD;
log_return_AD       = 100*252*results.log_return_AD;
T                   = results.T;
caldt_daily         = results.caldt;

% ----------------------------------------------------------------% 
% LOAD TRADES
% ----------------------------------------------------------------% 

data = load('../01_Data/es_trades_5minute_EST.mat');

No_trades                                                                       = data.trades_per_min_mat;
No_contracts                                                                    = data.trade_quantity_per_min_mat;
Signed_volume                                                                   = data.signed_vol_per_min_mat;

beg_TRADES                                                                      = find_closest(data.caldt_daily,start_date);
fin_TRADES                                                                      = find_closest(data.caldt_daily,finish_date);

caldt_trades                                                                    = data.caldt_daily(beg_TRADES:fin_TRADES);
No_trades                                                                       = No_trades(beg_TRADES:fin_TRADES,:);
Volume                                                                          = No_contracts(beg_TRADES:fin_TRADES,:);
Signed_volume                                                                   = Signed_volume(beg_TRADES:fin_TRADES,:);


% ----------------------------------------------------------------% 
% INTERSECT
% ----------------------------------------------------------------% 

[C,IA,IB]                               = intersect(caldt_trades,caldt_daily);
caldt_daily                             = caldt_daily(IB);
log_return_HbH_AD                       = log_return_HbH_AD(IB,:);
log_return_AD                           = log_return_AD(IB,:);

caldt_trades                            = caldt_trades(IA);
No_trades                               = No_trades(IA,:);
Volume                                  = Volume(IA,:);
Signed_volume                           = Signed_volume(IA,:);

% 15 min intervals
indx                                    = [3 : 3 : length(Signed_volume(1,:))];
indx_loop                               = [0 , indx];
Signed_volume_15b15_AD                  = nan(size(Signed_volume,1),24*4);
Volume_15b15_AD                         = nan(size(Volume,1),24*4);

for H = 1 : round(length(Signed_volume(1,:))/12*4)
    Signed_volume_15b15_AD(:,H)         = sum(Signed_volume(:,indx_loop(H)+1 : indx_loop(H+1)),2);
    Volume_15b15_AD(:,H)                = sum(Volume(:,indx_loop(H)+1 : indx_loop(H+1)),2);
end

lead                                                = 1;
sum_signedVol_start                                 = 86;
sum_signedVol_end                                   = 89;

log_return_HbH_AD_lead                              = log_return_HbH_AD(1+lead:end,:);
log_return_AD_lead                                  = log_return_AD(1+lead:end,:);
close_signed_volume_lead                            = sum(Signed_volume_15b15_AD(1:end-lead,sum_signedVol_start:sum_signedVol_end),2);
close_volume_lead                                   = sum(Volume_15b15_AD(1:end-lead,sum_signedVol_start:sum_signedVol_end),2);
indx_no_trading_EOD_volume_lead                     = find(close_volume_lead < 100);
close_volume_lead(indx_no_trading_EOD_volume_lead)  = nan;
close_rel_signed_volume_lead                        = close_signed_volume_lead./close_volume_lead;

Volume_lead                                         = Volume(1+lead:end,:);
Signed_volume_lead                                  = Signed_volume(1+lead:end,:);
RSV_lead                                            = Signed_volume_lead./Volume_lead;

caldt_true_lead                                     = caldt_daily(1+lead:end);

caldt_true                                          = caldt_daily;
close_signed_volume                                 = sum(Signed_volume_15b15_AD(:,sum_signedVol_start:sum_signedVol_end),2);
close_volume                                        = sum(Volume_15b15_AD(:,sum_signedVol_start:sum_signedVol_end),2);
indx_no_trading_EOD_volume                          = find(close_volume < 100);
close_volume(indx_no_trading_EOD_volume)            = nan;
close_rel_signed_volume                             = close_signed_volume./close_volume;

% ----------------------------------------------------------------% 
% Sorts
% ----------------------------------------------------------------% 



%% fixed buckets

portfolios = [-1,-0.08,-0.04,-0.02,-0.01,0,0.01,0.02,0.04,0.08,1000000000];

%%
NN = length(portfolios)-1;
[caldt_close_rel_signedVol_sorted_lead , closing_rel_signedVol_sorted_lead]     = sort_High_Low_fixed(caldt_true_lead,close_rel_signed_volume_lead,portfolios);
[caldt_close_rel_signedVol_sorted , closing_rel_signedVol_sorted]               = sort_High_Low_fixed(caldt_true,close_rel_signed_volume,portfolios);

% Check:
for p = 1 : NN
    min(closing_rel_signedVol_sorted_lead{p,1});
    max(closing_rel_signedVol_sorted_lead{p,1});
end

percentage = 100*length(find(closing_rel_signedVol_sorted_lead{2,1} >0))/length(closing_rel_signedVol_sorted_lead{2,1});

% ----------------------------------------------------------------% 
% find high ... low date intersections
% ----------------------------------------------------------------% 

for p = 1 : NN
    
    [C,IA,IB]                               = intersect(caldt_close_rel_signedVol_sorted{p,1},caldt_true);
    caldt_close_rel_signedVol_sorted{p,1}   = caldt_close_rel_signedVol_sorted{p,1}(IA,:);
    closing_rel_signedVol_sorted{p,1}       = closing_rel_signedVol_sorted{p,1}(IA,:);
    caldt_daily_CTC_sorted{p,1}             = caldt_true(IB);
    
    % contemporaneous
    log_return_HbH_AD_sorted{p,1}           = log_return_HbH_AD(IB,:);
    log_return_AD_sorted{p,1}               = log_return_AD(IB,:); 
    
    closing_volume_sorted{p,1}              = close_volume(IB,:); 
    closing_SV_sorted{p,1}                  = close_signed_volume(IB,:); 
    closing_RSV_sorted{p,1}                 = close_rel_signed_volume(IB,:); 
    
end


for p = 1 : NN

    [C,IA,IB]                                       = intersect(caldt_close_rel_signedVol_sorted_lead{p,1},caldt_true_lead);
    caldt_close_rel_signedVol_sorted_lead{p,1}      = caldt_close_rel_signedVol_sorted_lead{p,1}(IA,:);
    closing_rel_signedVol_sorted_lead{p,1}          = closing_rel_signedVol_sorted_lead{p,1}(IA,:);
    caldt_daily_CTC_sorted_lead{p,1}                = caldt_true_lead(IB);

    % next day
    log_return_HbH_AD_sorted{p,2}                   = log_return_HbH_AD_lead(IB,:);
    log_return_AD_sorted{p,2}                       = log_return_AD_lead(IB,:); 

    Volume_lead_sorted{p,1}                         = Volume_lead(IB,:); 
    Signed_volume_lead_sorted{p,1}                  = Signed_volume_lead(IB,:); 
    RSV_lead_sorted{p,1}                            = RSV_lead(IB,:); 

end

% averages
for p = 1 : NN
    
    temp                        = nansum(log_return_AD_sorted{p,2}(:,97:108),2);
    rets_OD(p,1)                = nanmean(temp);
    [~,~,ci,stats]              = ttest(temp,0,'Alpha',0.05);
    rets_OD(p,2)                = ci(1);
    rets_OD(p,3)                = ci(2);
    
    temp                        = nansum(log_return_AD_sorted{p,2}(:,1:96),2);
    rets_Asia(p,1)              = nanmean(temp);
    [~,~,ci,stats]              = ttest(temp,0,'Alpha',0.05);
    rets_Asia(p,2)              = ci(1);
    rets_Asia(p,3)              = ci(2);
    
    temp                        = nansum(log_return_AD_sorted{p,2}(:,97:120),2);
    rets_EU(p,1)                = nanmean(temp);
    [~,~,ci,stats]              = ttest(temp,0,'Alpha',0.05);
    rets_EU(p,2)                = ci(1);
    rets_EU(p,3)                = ci(2);
    
end

% tests

rets_OD(:,2)                = rets_OD(:,2) - rets_OD(:,1);
rets_OD(:,3)                = rets_OD(:,3) - rets_OD(:,1);

rets_Asia(:,2)              = rets_Asia(:,2) - rets_Asia(:,1);
rets_Asia(:,3)              = rets_Asia(:,3) - rets_Asia(:,1);

rets_EU(:,2)                = rets_EU(:,2) - rets_EU(:,1);
rets_EU(:,3)                = rets_EU(:,3) - rets_EU(:,1);


x = 1 : NN;
     

figure
p           = bar(x, rets_Asia(:,1),'stacked');
p.BarWidth  = 1;
p.FaceColor = rgb('LightSteelBlue');
xticks(x)
xticklabels({'-8%<','-8% : -4%','-4% : -2%','-2% : -1%','-1% : 0%'...
    ,'0% : 1%','1% : 2%','2% : 4%','4% : 8%','8%>'})
xtickangle(90)
hold on
er = errorbar(x,rets_Asia(:,1),rets_Asia(:,2),rets_Asia(:,3));    
er.Color = [0 0 0];                            
er.LineStyle = 'none';  
er.LineWidth = 2;  
grid on 
legend('Asia')


figure
p                = bar(x, rets_EU(:,1),'stacked');
p.BarWidth       = 1;
p.FaceColor      = rgb('PowderBlue');
xticks(x)
xticklabels({'-8%<','-8% : -4%','-4% : -2%','-2% : -1%','-1% : 0%'...
    ,'0% : 1%','1% : 2%','2% : 4%','4% : 8%','8%>'})
xtickangle(90)
hold on
er = errorbar(x,rets_EU(:,1),rets_EU(:,2),rets_EU(:,3));    
er.Color = [0 0 0];                            
er.LineStyle = 'none';  
er.LineWidth = 2;  
grid on 
legend('Europe')

         

figure
p               = bar(x, rets_OD(:,1),'stacked');
p.BarWidth      = 1;
p.FaceColor     = rgb('SkyBlue');
xticks(x)
xticklabels({'-8%<','-8% : -4%','-4% : -2%','-2% : -1%','-1% : 0%'...
    ,'0% : 1%','1% : 2%','2% : 4%','4% : 8%','8%>'})
xtickangle(90)
hold on
er = errorbar(x,rets_OD(:,1),rets_OD(:,2),rets_OD(:,3));    
er.Color = [0 0 0];                            
er.LineStyle = 'none';  
er.LineWidth = 2;  
grid on 
legend('OD')

